package com.ys.controller.web.hr;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.hr.TwlStockTransferService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.hr.TwlStockTransfer;
import com.ys.entity.hr.TwlStockTransferDetail;
import com.ys.entity.sys.TsOrganizationInfo;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/hr/twlStockTransfer")
public class WebTwlStockTransferController {
	
	@Autowired
	private CommonApiService<TwlStockTransfer> commonApiService;


	@Autowired
	private TwlStockTransferService twlStockTransferService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TWL_StockTransfer);
			
			TwlStockTransfer entity = new TwlStockTransfer()
					.setBid(bid)
					.setCreateTime(new Date());
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TWL_StockTransfer, new ArrayList<Map<String, Object>>());
			
			return commonApiService.toAdd(model, entity, MenuId.TWL_StockTransfer, null, subGridConfig);
			
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 审核
	 */
	@RequiresPermissions("twlStockTransfer:add")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}

		try {
			TwlStockTransfer entity = new TwlStockTransfer().setBid(bid).queryFirst();
			TsOrganizationInfo org = new TsOrganizationInfo().setOrgid(entity.getOrgidTo()).queryFirst();

			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("orgNameTo", org.getOrgName());



			// 子列表配置信息
			List<Map<String, Object>> sonlist = twlStockTransferService.getItemList(bid);

			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TWL_StockTransfer, sonlist);
			return commonApiService.toEdit(model, entity.getId(), new TwlStockTransfer(), MenuId.TWL_StockTransfer, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}